package com.duan.boot.controller;

import com.duan.boot.common.R;
import com.duan.boot.entity.News;
import com.duan.boot.service.NewsPlusService;
import com.duan.boot.service.NewsService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/v1/oa/news")
public class NewsController {
    private final NewsService newsService;

    private final NewsPlusService newsPlusService;

    @Autowired
    public NewsController(NewsService newsService, @Qualifier("newsPlusServiceImpl") NewsPlusService newsPlusService) {
        this.newsService = newsService;
        this.newsPlusService=newsPlusService;
    }



    @ApiOperation(value = "增加任务")
    @PostMapping("/add")
    public R addOaNews(@RequestBody(required = true) News news) {
        Integer i = newsService.insertOaNews(news);
        return R.ok(i>0 ? "success" :"failed");
    }

    @ApiOperation(value = "删除任务")
    @DeleteMapping("/delete/{id}")
    public R removeById(@PathVariable("id") Long id) {

        boolean remove = newsPlusService.removeById(id);
        return R.ok(remove ? "success" : "error");
    }

    @ApiOperation(value = "更新任务")
    @PutMapping("/save")
    public R updateNews(@RequestBody(required = true) News news) {

//        Long id = news.getNewsId();
//        News oaNews = newsPlusService.getById(id);
//        BeanUtils.copyProperties(news,oaNews);
        Boolean res = newsPlusService.updateById(news);
        return R.ok(res);
    }

    @ApiOperation(value  = "查找所有的新闻")
    @GetMapping("/findAll")
    public R findAll(){
        return R.ok(newsService.findAll());
    }

    @ApiOperation(value = "根据任务的类型查找那条新闻")
    @RequestMapping(value = "/getByType", method = RequestMethod.GET)
    public R getByType(@RequestParam Integer typeid){
        return newsService.getByType(typeid)==null ? R.fail("没有这种类型的通知") : R.ok(newsService.getByType(typeid));
    }

    @ApiOperation("查询任务个数")
    @RequestMapping(value = "/numberOfNews", method = RequestMethod.GET)
    public R numberOfDep(){
        return R.ok(newsService.getNumberOfNews());
    }

    @ApiOperation("分页查询")
    @RequestMapping(value = "/pagedQuery", method = RequestMethod.GET)
    public R pagedQuery(@RequestParam Integer page,
                        @RequestParam Integer numberOfPage){
        return R.ok(newsService.pagedQuery(page,numberOfPage));
    }

    @ApiOperation("分页查询页码个数")
    @RequestMapping(value = "/pagedQueryPage", method = RequestMethod.GET)
    public R pagedQueryPage(@RequestParam Integer numberOfPage){
        return R.ok(newsService.pagedQueryPage(numberOfPage));
    }
}
